package com.briup.oagg.model.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQExchangeConfig {
    @Autowired
    private RabbitConfigProperties properties;

    @Bean
    public MessageConverter jsonMessageConvert(){
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        converter.setAlwaysConvertToInferredType(true);
        return converter;
    }

    @Bean
    public Queue clusterQueue(){
        return new Queue(properties.getSendQueue(),true);
    }

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(properties.getSendExchange(),true,false);
    }

    @Bean
    public Binding bindingDirectExchange(){
            return BindingBuilder.bind(clusterQueue()).to(directExchange()).with(properties.getSendRoutingKey());
    }
}
